//
//  ZCStatusPicView.swift
//  weibo
//
//  Created by Ning Xie on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

let itemMargin :CGFloat = 5
let itemWidth = (UIScreen.mainScreen().bounds.size.width - 2*StatusTableViewCellMargin - 2*itemMargin)/3


class ZCStatusPicView: UICollectionView {
    
    var picUrls: [ZCStatusPicModel]? {
        didSet{
            
            let size = calcSizeWithCount(picUrls?.count ?? 0)
            
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            
            self.reloadData()
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        dataSource = self
        
        registerClass(ZCStatusPicViewCell.self, forCellWithReuseIdentifier: "cell")
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //计算配图视图的大小
    private func calcSizeWithCount(count:Int)-> CGSize{
        
        let cols = count>3 ? 3 : count
        let row = (count - 1)/3 + 1
        
        let currentWidth = itemWidth * CGFloat(cols) + itemMargin * CGFloat(cols - 1)
        let currentHeight = itemWidth * CGFloat(row) + itemMargin * CGFloat(row - 1)
        
        return CGSizeMake(currentWidth, currentHeight)
    }

}

extension ZCStatusPicView :UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ZCStatusPicViewCell
     
        cell.picUrl = picUrls![indexPath.item]
        return cell
    }

    
}


class ZCStatusPicViewCell : UICollectionViewCell{
    
    var picUrl: ZCStatusPicModel?{
     
        didSet{
            if let url = picUrl?.bmiddle_pic {
                
                imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            }
        }
    }
    
    //配图控件
    var imageView: UIImageView = {
       
        var view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        view.contentMode = UIViewContentMode.ScaleAspectFill
        view.clipsToBounds = true
        
        return view
    }()
    
           
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(imageView)
        
        imageView.snp_updateConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
    }
    
}

